//
//  STChannelCellModel.swift
//  StarTimesON
//
//  Created by Toj on 9/23/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class STChannelCellModel: NSObject {
    
    // 支付渠道ID
    var id = 0
    // 支付渠道名称
    var name = ""
    
    var isOnlyOne = false
    
    private var name_ = ""
    // 支付渠道图标
    private(set) var iconUrl = ""
    // 银行卡图标
    private(set) var cardIcon = ""
    // 钱包
    private(set) var ewallet = ""
    // 支付渠道协议类型
    private(set) var appInterfaceMode: ChannelMode = .others
    // 支付渠道类型
    private(set) var payType = 0
    // 支付渠道code
    private(set) var code: String = ""
    // 支付渠道授权code
    private(set) var authorizationCode: String = ""
    
    /** 表单配置项是否存在（是否需要收集用户信息） **/
    private(set) var formConfigExist = false
    // 钱包信息
    private(set) var account: PayAccount?
    
    private(set) var message = ""
    
    // 优惠渠道促销活动
    private(set) var promotionTag = ""
    private(set) var promotionDesc = ""
    // (优惠券 + 支付渠道)优惠后的价格
    var actualPayAmount: Double = 0
    // 规格是否支持优惠
    private(set) var isPayPromotion = false
    // 支付渠道优惠id
    private(set) var payPromotionId = ""
    
    // 默认选中项
    var isSelected = false
    
    /// 是否是添加卡的cell
    var isAddCarCell = false
    /// 是否是卡的cell
    var isCarCell:Bool = false
    /// 是否是卡的Header
    var isCarTitleCell = false
    /// 当前货币符号 与规格货币是否一致
    var walletSymbolEqual = true
    // 支付渠道: 是否是ApplePay
    var isApplePay: Bool { return code.contains("AppleIAP") }
    
    // 支付渠道: 是否是钱包
    var isEWallet: Bool { return code.contains("MobileWallet") }
    // 钱包支付渠道是否展示充值入口
    var isNeedRecharge: Bool {
        guard let account = account else { return false }
        // print("-->toj: isN isEWallet:\(isEWallet)")
        // print("-->toj: isN walletSymbolEqual:\(walletSymbolEqual)")
        // print("-->toj: isN isAmount:\(actualPayAmount > account.amount)")
        // 钱包 && 钱包符号一致 && 商品金额 > 钱包金额
        return isEWallet && walletSymbolEqual && actualPayAmount > account.amount
    }
    // 钱包支付渠道是否可以正常支付
    var isCanPay: Bool {
        guard let _ = account else { return false }
        // 钱包 && 钱包符号一致 && 商品金额 <= 钱包金额
        //print("-->toj: CanPay walletSymbolEqual:\(walletSymbolEqual)")
        //print("-->toj: CanPay isNeedRecharge:\(isNeedRecharge)")
        return walletSymbolEqual && !isNeedRecharge
    }
    // 依附支付渠道
    private(set) var dependChannel: STChannelCellModel?
    
    /**
     *  payChannel: 支付渠道
     *  channel: 简易的支付渠道
     */
    init(payChannel: PayChannelDto, channel: CommodityPayChannel? = nil) {
        super.init()
        id = payChannel.id
        code = payChannel.code
        appInterfaceMode = payChannel.appInterfaceMode
        payType = payChannel.payType
        formConfigExist = payChannel.formConfigExist
        message = payChannel.message
        
        iconUrl = payChannel.logoUrl
        promotionDesc = payChannel.payPromotion?.promotionDesc ?? ""
        promotionTag = payChannel.payPromotion?.promotionTag ?? ""
        isCarTitleCell = payChannel.isSupportCardBind && STUserModel.shared.isLogin()
        payPromotionId = payChannel.payPromotion?.payPromotionId ?? ""
        // OTT 商品支付渠道根据规格切换变化的, 有简易规格信息
        // TV  商品 支付渠道是固定的, 没有简易规格信息
        if let chnl = channel {
            // isPayPromotion = chnl.isPayPromotion
            actualPayAmount = chnl.actualPayAmount
        }
        name_ = payChannel.name
        name = name_
        
        if !isEWallet { return }
        guard let aAccount = payChannel.account else { return }
        eWalletAccount(aAccount)
    }
    
    func eWalletAccount(_ aAccount: PayAccount) {
        account = aAccount
        var subName = ""
        if STUserModel.shared.isLogin() {
            let symbol = account?.currencySymbol ?? ""
            let amount = account?.amount ?? 0
            subName = ": " + String.currencySpaceFormate(sign: symbol, number: amount)
        }
        name = name_ + subName
    }
    
    /**
     *  payChannelCard: 银行卡信息
     *  channelModel  : 支付渠道
     */
    init(payChannelCard: PayChannelCardAuthDto, channelModel: STChannelCellModel) {
        super.init()
        // 支付渠道
        id = channelModel.id
        code = channelModel.code
        appInterfaceMode = channelModel.appInterfaceMode
        payType = channelModel.payType
        message = channelModel.message
        formConfigExist = channelModel.formConfigExist
        payPromotionId = channelModel.payPromotionId
        actualPayAmount = channelModel.actualPayAmount
        // 附属哪个支付渠道下面
        dependChannel = channelModel
        
        // 附属银行卡
        name = payChannelCard.brand
        if !payChannelCard.last4.isEmpty
        { name += payChannelCard.last4 }
        
        let type = PayCardType(rawValue: payChannelCard.brand) ?? .other
        cardIcon = type.imageStr()
        isCarCell = true
        authorizationCode = payChannelCard.authorizationCode
    }
}
